#!/usr/bin/node

const fs = require('fs'),
      a = process.argv[2],
      b = process.argv[3];

//cp a b


if(typeof(a) === 'undefined') {
  console.error('请正确输入命令行参数,源文件');
  process.exit(1);
}
if(typeof(b) === 'undefined') {
  console.error('请正确输入命令行参数，目标文件');
  process.exit(2);
}
//源文件a不存在就打印错误信息
if(!fs.existsSync(a)) {
  console.error('%s not exist!', a);
  process.exit(3);
}
//不是文件，
if(!fs.statSync(a).isFile()) {
  console.error('%s is not a file', a);
  process.exit(4);
}

//复制文件内容
//var stm = fs.createReadStream(a).pipe(fs.createWriteStream(b));
let aStream = fs.createReadStream(a);
let bStream = fs.createWriteStream(b);

aStream.pipe(bStream);
//复制文件权限
//stm.on('close', () => {

bStream.on('close', () => {
  fs.chmodSync(b, fs.statSync(a).mode);
});
